package com.ticket.sass.admin.config;

import com.ticket.sass.admin.util.BCryptUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ywb
 * @date 2022-09-26 14:55
 */
@Slf4j
@Configuration
public class AppConfig {
    public static String KEY_PREFIX;
    
    public static String PWD_SECRET;
    
    public static String CLIENT_SECRET;
    
    public static String TOKEN_SECRET;
    
    public final static Map<String, FilePathInfo> FILE_NAME_MAP = new HashMap<>() {
        {
            put("cinema-logo", new FilePathInfo("cinema/logo/", true));
            put("wx-cover", new FilePathInfo("wxa/cover/", false));
            put("cinema-config", new FilePathInfo("cinema/config/", true));
            put("banner", new FilePathInfo("banner/", true));
            put("member-cover", new FilePathInfo("member/cover/", true));
            put("commodity", new FilePathInfo("commodity/", true));
            put("commodity-category-icon", new FilePathInfo("commodity/category/icon/", true));
            put("commodity-guide", new FilePathInfo("commodity/guide/", false));
            put("member-recharge", new FilePathInfo("member/card/recharge/", true));
        }
    };
    

    
    public final static String VALID_LOGIN_COUNT_IP_KEY = "ticket-admin-ip:%s";
    
    @Value("${config.key.prefix}")
    public void setKeyPrefix(String param) {
        KEY_PREFIX = param;
    }
    
    @Value("${config.jwt.secret}")
    public void setTokenSecret(String param) {
        TOKEN_SECRET = param;
    }
    
    @Value("${config.pwd.secret}")
    public void setPwdSecret(String param) {
        PWD_SECRET = param;
    }
    
    @Value("${config.client.secret}")
    public void setClientSecret(String param) {
        CLIENT_SECRET = param;
    }
    
    @PostConstruct
    void init(){
        log.info("管理员默认密码【{}】", new BCryptUtil(PWD_SECRET).encrypt("123456"));
    }
    
    public static class FilePathInfo {
        private String path;
        private boolean isTemporary;
        
        public FilePathInfo(String path, boolean isTemporary) {
            this.path = path;
            this.isTemporary = isTemporary;
        }
        
        public String getPath() {
            return path;
        }
        
        public boolean isTemporary() {
            return isTemporary;
        }
    }
    
}
